package itheima_edu_info_manager.com.itheima.edu.info.manager.service;

import itheima_edu_info_manager.com.itheima.edu.info.manager.dao.TeacherDao;
import itheima_edu_info_manager.com.itheima.edu.info.manager.domain.Teacher;

public class TeacherService {
    private TeacherDao teacherDao = new TeacherDao();

    //增加老师
    public boolean addTeacher(Teacher teacher) {
        return teacherDao.addTeacher(teacher);
    }

    //删除老师
    public void deleteTeacherById(String id) {
        teacherDao.deleteTeacherById(id);
    }

    //修改老师
    public void upDateTeacherById(String id, Teacher teacher) {
        teacherDao.upDateTeacherById(id,teacher);
    }

    //查询老师
    public Teacher[] findAllTeacher() {
        Teacher[] teacher = teacherDao.findAllTeacher();
        boolean flag = false;
        //遍历数组
        for (int i = 0; i < teacher.length; i++) {
            Teacher te = teacher[i];
            //判断数组内是否存在数据
            if (te != null) {
                flag = true;
                //***如果存在数据，则需要将循环结束***
                break;
            }
        }
        if (flag) {
            return teacher;
        } else {
            return null;
        }
    }

    //判断输入的id是否存在
    public boolean isExists(String id) {
        //假设输入的id是不存在
        boolean flag = false;
        Teacher[] teacher = teacherDao.findAllTeacher();
        //遍历数组
        for (int i = 0; i < teacher.length; i++) {
            Teacher te = teacher[i];
            //判断数组是否为null，并且判断id是否存在。
            if (te !=null && te.getId().equals(id)){
                flag = true;
                break;
            }
        }
        return flag;
    }
}
